package batch_kube

import (
	"bufio"
	"fmt"
	"golang_study/internal/util"
	"k8s.io/client-go/kubernetes"
	"os"
	"reflect"
	"strconv"
	"sync"
)

func interactiveResource(ns string, fun delConfigmapFun, clientSet *kubernetes.Clientset) {
	podInfoChan := make(chan []PodInfo)
	var wg sync.WaitGroup
	type getPod func(string, *kubernetes.Clientset)
	t := reflect.TypeOf(fun)
	switch t {
	default:
		fmt.Println(t)
	}

	go func(podInfoChan chan []PodInfo) {
		for {
			wg.Add(1)
			podInfo := GetPods(ns, clientSet)
			podInfoChan <- podInfo
			wg.Wait()
		}
	}(podInfoChan)

	for {
		podInfo := <-podInfoChan
		fmt.Printf("Which one do you want to delete? [No|No1,No2..|Name|Name1,Name2..|all] ")

		reader := bufio.NewReader(os.Stdin)
		input, err := reader.ReadString('\n')
		if err != nil {
			fmt.Printf("Fail is reason that, %s\n", err)
		}

		items := util.InitArgs(input)
		if items == nil {
			fmt.Printf("Missing value for input.\n")
		} else if util.FindMaxNumber(items) > len(podInfo) {
			fmt.Printf("Number is invalid, %d\n", util.FindMaxNumber(items))
		} else {
			for _, index := range items {
				if index == "all" {
					for _, pod := range podInfo {
						fun(pod.Namespace, pod.Name, clientSet)
					}
					break
				}
				i, _ := strconv.Atoi(index)
				fun(podInfo[i-1].Namespace, podInfo[i-1].Name, clientSet)
			}
		}

		wg.Done()
	}
}
